package com.abc.robot01.activity

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.abc.robot01.activity.HuXingTiaoZheng.Companion
import com.abc.robot01.activity.HuXingTiaoZheng.WebAppInterface
import com.abc.robot01.databinding.FragmentH52Binding
import com.abc.robot01.retrofit.RetrofitService.Companion.BASE_URL_H5
import com.abc.robot01.vivemodel.Robot

/**
 * 商品库
 */
class H5Activity : BaseVoiceActivity() {
    private lateinit var binding: FragmentH52Binding
    private lateinit var webView: WebView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
         val  url = intent.getStringExtra("url")!!+"&showType=6";
        val id  =  intent.getStringExtra("id")!!;
        Log.e(TAG, "url: "+url);
        binding = FragmentH52Binding.inflate(layoutInflater);
        setContentView(binding.root)

        binding.composeView.setContent {
            MaterialTheme {
                Box {
                    Row(modifier = Modifier.padding(30.px)) {
                        BackButton {
                        }
                    }
                }
            }
        }

        webView=binding.webView;
        // 清除缓存
        webView.clearCache(true)
        webView.settings.javaScriptEnabled = true
        webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
        webView.settings.domStorageEnabled = true
        webView.webViewClient = WebViewClient()
        webView.loadUrl(url!!)
        webView.addJavascriptInterface(WebAppInterface(this@H5Activity,id), "AndroidInterface")
        }

    // JS 调用的接口
    class WebAppInterface(private val context: Context,val id:String) {
        @JavascriptInterface
        fun price() {
            Log.e(TAG, "price:")
            context.startActivity<BaoJia2Activity>() {
                val baseUrl = "${BASE_URL_H5}robot/imbedding/peicelist"
                val url3 = Uri.Builder()
                    .encodedPath(baseUrl)
                    .appendQueryParameter("access_token", "${Robot.accessToken}")
                    .appendQueryParameter("companyId", "${Robot.companyId}")
                    .appendQueryParameter("userId", "${Robot.userId}")
                    .appendQueryParameter("id", "${id}")
                    .build()
                    .toString()
                putExtra("url", url3.also {
                    Log.e(TAG, "url: "+it);
                }
                )
            }
        }
    }

    override fun onPause() {
        super.onPause()
        webView.evaluateJavascript("javascript:closeMusic();", null)
    }

    override fun onResume() {
        super.onResume()
        webView.evaluateJavascript("javascript:openMusic();", null)
    }

    companion object {
        const val TAG = "H5Activity"
    }
}

